package com.tywj.buscustomerapp.view.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ChangePwdActivity extends BaseActivity {
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.login_pwd)
    EditText pwdEdit;
    @BindView(R.id.login_repwd)
    EditText repwdEdit;
    @BindView(R.id.register_submit)
    TextView submit;
    private String phone;
    private LoadingDialog waitDialog;


    @Override
    protected int getContentId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("重置密码");
        if (getIntent()!=null){
            phone = getIntent().getStringExtra("phone");
        }
        if (phone==null || phone.equals("")){
            finish();
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = pwdEdit.getText().toString();
                String repwd = repwdEdit.getText().toString();
                if (pwd.equals("") || pwd.length()<8){
                    ToastJoe.getmToastJoe().ToastLongShow(ChangePwdActivity.this,null,"请输入八位以上密码");
                }else if (!repwd.equals(pwd)){
                    ToastJoe.getmToastJoe().ToastLongShow(ChangePwdActivity.this,null,"两次输入密码不一致，请重新输入");
                    repwdEdit.setText("");
                }else{
                    doChangePwd(phone,pwd);
                }
            }
        });
    }

    private void doChangePwd(final String phone, final String pwd){
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e){
                try {
                    String mes = WebServicrUtils.getInstance().doChangePwd(phone,pwd);
                    if (mes.equals("ok")){
                        e.onNext(mes);
                    }else{
                        e.onError(null);
                    }
                } catch (IOException e1) {
                    e.onError(e1);
                    e1.printStackTrace();
                } catch (XmlPullParserException e1) {
                    e.onError(e1);
                    e1.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (waitDialog==null)
                            waitDialog = new LoadingDialog(ChangePwdActivity.this);
                        if (!waitDialog.isShowing())
                            waitDialog.show();
                    }

                    @Override
                    public void onNext(String s) {
                        if (waitDialog!=null && waitDialog.isShowing())
                            waitDialog.dismiss();
                        ToastJoe.getmToastJoe().ToastShowRight(ChangePwdActivity.this,null,"重置密码成功");
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (waitDialog!=null && waitDialog.isShowing())
                            waitDialog.dismiss();
                        ToastJoe.getmToastJoe().ToastLongShow(ChangePwdActivity.this,null,"重置密码失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("重置密码");
        MobclickAgent.onResume(this); //统计时长
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("重置密码");
        MobclickAgent.onPause(this);
    }

}
